package com.ruoyi.electronicFence.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.electronicFence.domain.TEfEquipmentDeviceVo;
import com.ruoyi.electronicFence.domain.TEfEquipmentStatusVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.electronicFence.domain.TEfEquipmentDevice;
import com.ruoyi.electronicFence.service.ITEfEquipmentDeviceService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 设备或场景与监测设备关联管理 Controller
 *
 * @author sxygsj
 * @date 2024-05-29
 */
@RestController
@RequestMapping("/electronicFence/device")
public class TEfEquipmentDeviceController extends BaseController
{
    private final ITEfEquipmentDeviceService tEfEquipmentDeviceService;

    @Autowired
    public TEfEquipmentDeviceController(ITEfEquipmentDeviceService tEfEquipmentDeviceService) {
        this.tEfEquipmentDeviceService = tEfEquipmentDeviceService;
    }

    /**
     * 列表
     */
    //@PreAuthorize("@ss.hasPermi('electronicFence:device:list')")
    @GetMapping("/listV2")
    public TableDataInfo list(TEfEquipmentDevice tEfEquipmentDevice)
    {
        startPage();
        List<TEfEquipmentDevice> list = tEfEquipmentDeviceService.selectTEfEquipmentDeviceList(tEfEquipmentDevice);
        return getDataTable(list);
    }

    /**
     * 详情列表
     */
    //@PreAuthorize("@ss.hasPermi('electronicFence:device:list')")
    @GetMapping("/details/listV2")
    public TableDataInfo selectTEfEquipmentDeviceDetailsList(TEfEquipmentDevice tEfEquipmentDevice)
    {
        startPage();
        List<TEfEquipmentDeviceVo> list = tEfEquipmentDeviceService.selectTEfEquipmentDeviceDetailsList(tEfEquipmentDevice);
        return getDataTable(list);
    }

    /**
     * 详情列表
     */
    //@PreAuthorize("@ss.hasPermi('electronicFence:device:list')")
    @GetMapping("/list")
    public TableDataInfo selectTEfEquipmentDeviceDetailsListV2(TEfEquipmentDevice tEfEquipmentDevice)
    {
        startPage();
        List<TEfEquipmentDeviceVo> list = tEfEquipmentDeviceService.selectTEfEquipmentDeviceDetailsListV2(tEfEquipmentDevice);
        return getDataTable(list);
    }


    /**
     * 导出设备或场景与监测设备关联  列表
     */
    //@PreAuthorize("@ss.hasPermi('electronicFence:device:export')")
    @Log(title = "设备或场景与监测设备关联  ", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, TEfEquipmentDevice tEfEquipmentDevice)
    {
        List<TEfEquipmentDevice> list = tEfEquipmentDeviceService.selectTEfEquipmentDeviceList(tEfEquipmentDevice);
        ExcelUtil<TEfEquipmentDevice> util = new ExcelUtil<>(TEfEquipmentDevice.class);
        util.exportExcel(response, list, "设备或场景与监测设备关联  数据");
    }

    /**
     * 获取设备或场景与监测设备关联  详细信息
     */
    //@PreAuthorize("@ss.hasPermi('electronicFence:device:query')")
    @GetMapping(value = "/v2/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {

        TEfEquipmentDevice tEfEquipmentDevice = tEfEquipmentDeviceService.selectTEfEquipmentDeviceDetailsListById(id);

        return success(tEfEquipmentDeviceService.selectTEfEquipmentDeviceDetailsListById(id));
    }

    /**
     * 获取设备或场景与监测设备关联  详细信息
     */
    //@PreAuthorize("@ss.hasPermi('electronicFence:device:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfoV2(@PathVariable("id") Long id)
    {
        return success(tEfEquipmentDeviceService.selectTEfEquipmentDeviceById(id));
    }


    /**
     * 新增
     */
    //@PreAuthorize("@ss.hasPermi('electronicFence:device:add')")
    @Log(title = "新增", businessType = BusinessType.INSERT)
    @PostMapping("/insert")
    public AjaxResult add(@RequestBody TEfEquipmentDevice tEfEquipmentDevice)
    {
        return toAjax(tEfEquipmentDeviceService.insertTEfEquipmentDevice(tEfEquipmentDevice));
    }

    /**
     * 新增关联
     */
    //@PreAuthorize("@ss.hasPermi('electronicFence:device:add')")
    @Log(title = "新增", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add2(@RequestBody TEfEquipmentDevice tEfEquipmentDevice)
    {
        return tEfEquipmentDeviceService.insertEquipmentMonitorDevice(tEfEquipmentDevice);
    }

    /**
     * 修改
     */
    //@PreAuthorize("@ss.hasPermi('electronicFence:device:edit')")
    @Log(title = "修改  ", businessType = BusinessType.UPDATE)
    @PutMapping("/update")
    public AjaxResult edit(@RequestBody TEfEquipmentDevice tEfEquipmentDevice)
    {
        return toAjax(tEfEquipmentDeviceService.updateTEfEquipmentDevice(tEfEquipmentDevice));
    }

    /**
     * 修改
     */
    //@PreAuthorize("@ss.hasPermi('electronicFence:device:edit')")
    @Log(title = "修改  ", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult editV2(@RequestBody TEfEquipmentDevice tEfEquipmentDevice)
    {
        return tEfEquipmentDeviceService.updateTEfEquipmentDeviceV3(tEfEquipmentDevice);
    }

    /**
     * 删除
     */
    //@PreAuthorize("@ss.hasPermi('electronicFence:device:remove')")
    @Log(title = "删除  ", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(tEfEquipmentDeviceService.deleteTEfEquipmentDeviceByIds(ids));
    }


    /**
     *  列表
     */
    //@PreAuthorize("@ss.hasPermi('electronicFence:device:list')")
    @GetMapping("/equipment/status2")
    public TableDataInfo getEquipmentStatus()
    {
        startPage();
        List<TEfEquipmentStatusVo> list = tEfEquipmentDeviceService.getEquipmentStatus();
        return getDataTable(list);
    }

    /**
     * 获取所有设备的状态信息，并根据最近的历史记录更新其在线/离线状态。
     *
     * 此方法首先获取所有设备的基础信息，然后通过查询每个设备的最新历史状态记录，
     * 判断设备是否在最近5分钟内有活动。如果有，则更新设备状态；如果没有，则标记为离线。
     *
     * @return 返回设备列表，每个设备带有最新的状态信息。
     */
    //@PreAuthorize("@ss.hasPermi('electronicFence:device:list')")
    @GetMapping("/equipment/status")
    public TableDataInfo getEquipmentStatus2(TEfEquipmentDevice tEfEquipmentDevice)
    {
        startPage();
        List<TEfEquipmentDeviceVo> list = tEfEquipmentDeviceService.getEquipmentStatusV3(tEfEquipmentDevice);
        return getDataTable(list);
    }



}
